 /*
 * ContribuyentesBusquedaFrame.java
 *
 * Created on 16 de abril de 2008, 10:08 AM
 */
package org.contabilidad.exoneraciones;

import org.mybeans.Sesion;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.Format;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  avbravo
 */
public class ExoneracionesMostrarFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    int Anio;

    /** Creates new form ContribuyentesBusquedaFrame */
    public ExoneracionesMostrarFrame() {
        initComponents();
        sesion = new Sesion();
        /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */
        modelo = (DefaultTableModel) jXTable1.getModel();
        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        int year = ca.get(java.util.Calendar.YEAR);
        this.Anio = year;
        jTextFieldAnio.setText(String.valueOf(Anio));
        String sql = "select exoneraciones.*  from exoneraciones  where idmunicipio ='" + sesion.getIdmunicipio() + "' and activo ='si' and year(fechaexoneracion)='" + Anio + "'";
        CargarTodosRegistros(sql);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldAnio = new javax.swing.JTextField();
        jButtonMostrar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mostrar Exoneraciones");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
         

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Fecha", "Autoriza", "Valor", "Porcentaje", "Detalle"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jXTable1.setOpaque(false);
        jScrollPane1.setViewportView(jXTable1);

        jLabel1.setText("Año");

        jTextFieldAnio.setText("0");
        jTextFieldAnio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldAnioActionPerformed(evt);
            }
        });

        jButtonMostrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/tools.png"))); // NOI18N
        jButtonMostrar.setToolTipText("Procesar");
        jButtonMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMostrarActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButton1.setToolTipText("Regresar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonMostrar)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMostrarActionPerformed
        // TODO add your handling code here:
        Procesar();

}//GEN-LAST:event_jButtonMostrarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextFieldAnioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldAnioActionPerformed
        // TODO add your handling code here:
        Procesar();

    }//GEN-LAST:event_jTextFieldAnioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ExoneracionesMostrarFrame().setVisible(true);
            }
        });
    }

    private void Procesar() {
        try {
            LimpiarTable();
            String sAnio = jTextFieldAnio.getText();
            if (sAnio == null || sAnio.equals("")) {
                sesion.MostrarMensajeInformacion("Mensaje", "Ingrese el año");

                return;
            }
            Anio = Integer.parseInt(sAnio);
            String sql = "select exoneraciones.*  from exoneraciones  where idmunicipio ='" + sesion.getIdmunicipio() + "' and activo ='si' and year(fechaexoneracion)='" + Anio + "'";
            CargarTodosRegistros(sql);
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "Clase: MorosidadContribuyentesAgregar.java\nGuardar()\n " + ex.getMessage().toString());
        }
    }

    public void CargarTodosRegistros(String sql) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(null, "No existen registros con esa condicion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }




// Bucle para cada resultado en la consulta
            rs.previous();
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[7]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idexoneracion");
                java.sql.Date mydate = rs.getDate("fechaexoneracion");
                Format formatter;
                formatter = new SimpleDateFormat("dd/MM/yyyy");
                String s = formatter.format(mydate);
                fila[1] = s;
                fila[2] = rs.getString("autorizado");
                fila[3] = rs.getString("valor");
                fila[4] = rs.getString("porcentaje");
                fila[5] = rs.getString("detalle");
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);
            //aris++
            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
//                        String cedula = (String) jTextFieldCedula.getText();
                        //                      String nombre = (String) jTextFieldNombre.getText();
                    }
                }
            });

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void LimpiarTable() {
        try {
            //limpiar  el jXTable

            boolean control = false;
            while (control == false) {
                int n = jXTable1.getRowCount();
                //System.out.println("n" + n);
                if (n <= 0) {
                    control = true;
                } else {
                    modelo.removeRow(0);
                }

            }//while
        } catch (Exception ex) {
            sesion.MostrarMensajeError("Error ", "LimpiarTable()\n " + ex.getMessage().toString());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonMostrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldAnio;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
